package com.xbongbong.paas.help.analysis.data;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.dto.AnalysisDataDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.parent.parse.data.FormDataListAnalysisDataHelp;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.OperateTypeEnum;
import com.xbongbong.saas.help.FormDataAnalysisHelp;
import com.xbongbong.saas.service.toolbox.help.AnalysisSaasDataListHelp;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * 解析所有的字段，用于除详情、编辑、列表、关联查询的回显
 * @author: wufeng
 * @date: 2018/12/11 15:17
 * @desrcption:
 */
@Component
public class FormDataDistributorAnalysisDataHelp extends FormDataListAnalysisDataHelp {

    @Resource
    private AnalysisSaasDataListHelp analysisSaasDataListHelp;

    /**
     * 之所以不使用FormDataListNoSubFormAnalysisDataHelp是因为，这边没有解析系统的关联业务字段等
     * @date 2019/7/4 15:23
     *
     */

    @Override
    public void analysisData(AnalysisDataDTO analysisDataDTO, JSONObject resultJsonObject, Map<String, Object> linkData
                    ,Set<Object> linkDataSet) throws XbbException {
        FieldAttrEntity fieldAttrEntity = analysisDataDTO.getFieldAttrEntity();
        String key = analysisDataDTO.getAttr();
        JSONObject objectJsonObject = analysisDataDTO.getDataJsonObject();
        if (Objects.isNull(analysisDataDTO.getIsProcess()) || !analysisDataDTO.getIsProcess()) {
            FormDataAnalysisHelp.invisibleSetNull(fieldAttrEntity, analysisDataDTO, objectJsonObject);
        }
        if (Objects.equals(FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(), fieldAttrEntity.getFieldType()) && Objects.nonNull(objectJsonObject.get(key))) {
            // 系统业务单选
            String linkedText = fieldAttrEntity.getLinkedText();
            JSONObject jsonObject = new JSONObject();
            JSONArray jsonArray = new JSONArray();
            jsonObject.put(StringConstant.SAAS_LINK_BUSINESS_ID, objectJsonObject.getLong(key));
            jsonObject.put(StringConstant.SAAS_LINK_BUSINESS_NAME, objectJsonObject.getString(linkedText));
            jsonObject.put(StringConstant.SAAS_LINK_BUSINESS_LINK_TYPE, fieldAttrEntity.getLinkedType());
            jsonObject.put("saasMark", SaasMarkEnum.SAAS.getCode());
            jsonArray.add(jsonObject);
            resultJsonObject.put(key, jsonArray);
        } else if (Objects.equals(FieldTypeEnum.LINK_BUSINESS_MULTI.getType(), fieldAttrEntity.getFieldType()) && Objects.nonNull(objectJsonObject.get(key))) {
            // 系统业务多选
            String linkedText = fieldAttrEntity.getLinkedText();
            JSONArray businessIdList  = objectJsonObject.getJSONArray(fieldAttrEntity.getAttr()) == null ? new JSONArray() : objectJsonObject.getJSONArray(fieldAttrEntity.getAttr());
            JSONArray businessNameList = objectJsonObject.getJSONArray(linkedText) == null ? new JSONArray() : objectJsonObject.getJSONArray(linkedText);
            if (!businessIdList.isEmpty() && !businessNameList.isEmpty()) {
                JSONArray jsonArray = new JSONArray();
                for (int i = 0; i < businessIdList.size(); i++) {
                    Long businessId = businessIdList.getLong(i);
                    String businessName = "";
                    if (CollectionsUtil.isNotEmpty(businessNameList) && i < businessNameList.size()) {
                        businessName = businessNameList.getString(i);
                    } else {
                        continue;
                    }
                    JSONObject linkObject = new JSONObject();
                    linkObject.put(StringConstant.SAAS_LINK_BUSINESS_ID, businessId);
                    linkObject.put(StringConstant.SAAS_LINK_BUSINESS_NAME, businessName);
                    linkObject.put(StringConstant.SAAS_LINK_BUSINESS_LINK_TYPE,fieldAttrEntity.getLinkedType());
                    linkObject.put("saasMark", SaasMarkEnum.SAAS.getCode());
                    jsonArray.add(linkObject);
                }
                resultJsonObject.put(key, jsonArray);
            }
        } else if (Objects.equals(FieldTypeEnum.BIRTHDAY.getType(), fieldAttrEntity.getFieldType())) {
            String linkedText = fieldAttrEntity.getLinkedText();
            analysisSaasDataListHelp.formatBirthday4Show(objectJsonObject, key, linkedText);
            resultJsonObject.put(key, objectJsonObject.get(key));
        } else if (Objects.equals(FieldTypeEnum.SWITCH.getType(), fieldAttrEntity.getFieldType())) {
            if (Objects.equals(objectJsonObject.getString(key), BasicConstant.ZERO.toString())) {
                resultJsonObject.put(key, OperateTypeEnum.NO.getName());
            }else if (Objects.equals(objectJsonObject.getString(key), BasicConstant.ONE.toString())){
                resultJsonObject.put(key, OperateTypeEnum.YES.getName());
            }else {
                resultJsonObject.put(key, null);
            }
        } else if (Objects.equals(FieldTypeEnum.LINK_RECEIVER.getType(), fieldAttrEntity.getFieldType()) && Objects.nonNull(objectJsonObject.get(key))) {
            // 合同关联收货人字段 关联子表单数据类型 特殊处理
            String shippName = objectJsonObject.getString(fieldAttrEntity.getLinkedText());
            resultJsonObject.put(key,shippName);
        } else {
            super.analysisData(analysisDataDTO, resultJsonObject, linkData, linkDataSet);
        }

    }
}
